package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeprService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeprService deptService;

    /**
     * 添加部门信息
     *
     * @param dept 部门对象，包含部门的详细信息
     * @return Result 操作结果，成功时返回成功状态
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("add,dept:{}", dept);
        // 调用服务层添加部门信息
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping
    public Result list(String name, Integer status, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("list,name:{},status:{},page:{},pageSize:{}", name, status, page, pageSize);
        PageResult<Dept> pageResult = deptService.list(name, status, page, pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("getById,id:{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping
    public Result upDate(@RequestBody Dept dept) {
        log.info("upDate,dept:{}", dept);
        deptService.upDate(dept);
        return Result.success();
    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("delete,id:{}", id);
        deptService.deleteById(id);
        return Result.success();
    }

    @GetMapping("list")
    public Result listAll() {
        List<Dept> dept = deptService.listAll();
        return Result.success(dept);
    }

}
